"""
专题模型
包括直播专题、宏观专题、证券专题、理财专题、会议专题、产经专题、基金专题
"""
from sqlalchemy import JSON, Column, Integer, String, Text, DateTime, SmallInteger, Enum
from sqlalchemy.ext.declarative import declarative_base
from .base import Base
from datetime import datetime
from sqlalchemy import event  
import json
class Topics(Base):
    __tablename__ = 'topics'
    id = Column(Integer, primary_key=True, autoincrement=True)
    title = Column(String(255), nullable=False)
    url = Column(String(255), unique=True, nullable=False) # 专题url
    source = Column(String(255), nullable=False)
    category = Column(Enum('直播', '宏观', '证券', '理财', '会议', '产经', '基金', name='category'), nullable=True)
    news_ids = Column(JSON, nullable=True) # 专题新闻列表[1, 2]
    created_time = Column(DateTime, default=datetime.now)
    updated_time = Column(DateTime, default=datetime.now, onupdate=datetime.now)


# 监听before_insert事件，在插入新闻记录前设置created_time和updated_time  
@event.listens_for(Topics, 'before_insert')  
def set_timestamps(mapper, connection, target):  
    target.created_time = datetime.now()  
    target.updated_time = datetime.now()  

# 监听before_update事件，在更新新闻记录前更新updated_time  
@event.listens_for(Topics, 'before_update')  
def update_timestamp(mapper, connection, target):  
    target.updated_time = datetime.now()